﻿using System;
using System.Collections.Generic;

namespace TallComponents.Web.Storage
{
   /// <summary>
   /// A proxy for IDictionary that allows only read-only access to the dictionary.
   /// </summary>
   /// <typeparam name="TKey">The type of the key.</typeparam>
   /// <typeparam name="TValue">The type of the value.</typeparam>
   public class ReadOnlyDictionary<TKey, TValue> : IEnumerable<TValue>
   {
      /// <summary>
      /// Initializes a new instance of the <see cref="ReadOnlyDictionary&lt;TKey, TValue&gt;"/> class.
      /// </summary>
      /// <param name="dictionary">The dictionary.</param>
      public ReadOnlyDictionary( IDictionary<TKey, TValue> dictionary )
      {
         if ( null == dictionary )
            throw new ArgumentNullException();

         _dictionary = dictionary;
      }

      /// <summary>
      /// Gets the value with the specified key.
      /// </summary>
      /// <value>The value.</value>
      /// <exception cref="ArgumentOutOfRangeException">If the key was not found in the collection.</exception>
      public TValue this[ TKey key ]
      {
         get { return _dictionary[ key ]; }
      }

      /// <summary>
      /// Tries to get the value with the specified key.
      /// </summary>
      /// <param name="key">The key.</param>
      /// <param name="value">The value.</param>
      /// <returns>
      /// A value indicating whether the key was found in the collection.
      /// </returns>
      public bool TryGetValue( TKey key, out TValue value )
      {
         return _dictionary.TryGetValue( key, out value );
      }

      /// <summary>
      /// Returns an enumerator that iterates through the collection.
      /// </summary>
      /// <returns>
      /// An <see cref="T:System.Collections.Generic.IEnumerator`1"/> that can be used to iterate through the collection.
      /// </returns>
      public IEnumerator<TValue> GetEnumerator()
      {
         return _dictionary.Values.GetEnumerator();
      }

      /// <summary>
      /// Returns an enumerator that iterates through a collection.
      /// </summary>
      /// <returns>
      /// An <see cref="T:System.Collections.IEnumerator"/> object that can be used to iterate through the collection.
      /// </returns>
      System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator()
      {
         return _dictionary.Values.GetEnumerator();
      }

      private readonly IDictionary<TKey, TValue> _dictionary;
   }
}
